import scrapy
from tutorial.items import *
from scrapy.http import Request


class DmozSpider(scrapy.Spider):
    name = "dmoz"
    allowed_domains = ["v.qq.com"]
    start_urls = [
        "http://v.qq.com/x/list/tv",

    ]
    url = "http://v.qq.com/x/list/tv"

    def parse(self, response):
        for sel in response.xpath('//ul/li'):
            item = DmozItem()
            item['title'] = sel.xpath('div/strong/a/text()').extract()
            item['actor'] = sel.xpath('div/a/text()').extract()
            if item['title']:
                item['title'] = item['title'][0]
                print(item['title'], item['actor'])
                yield item
                with open('video.txt', 'a') as f:
                    f.write(item['title'] +
                            '\t' + ' '.join(item['actor']) + '\n')

        nextlink = response.xpath(
            '//div/a[@class="page_next"]/@href').extract()
        if nextlink:
            link = nextlink[0]
            print("##############")
            print(self.url + link)
            print("##############")
            yield Request(self.url + link, callback=self.parse)
